<?php
if ( IPB_THIS_SCRIPT != 'admin' )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'init.php'.";
	exit();
}

class ad_taxes
{
	var $ipsclass;
	
	function auto_run()
	{
		$code_redirect = array(
								'list' => 'list_taxes',
								'add' => 'add_tax',
								'edit' => 'edit_tax',
								'save' => 'save_tax',
								'delete' => 'delete_tax',
								'log' => 'collected_taxes'
							);
		if(!$code_redirect[$this->ipsclass->input['do']])
		{
			$this->ipsclass->input['do'] = 'list';
		}
		$run = $code_redirect[$this->ipsclass->input['do']];
		$this->$run();		
	}
	
	function list_taxes()
	{
		$this->ipsclass->admin->page_title  = "Welcome managing taxes for ibMarket";
		$this->ipsclass->admin->page_detail = "Welcome managing taxes for ibMarket. Here you can add, set, delete taxes for money on hand or post gained.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=taxes', 'Manage Taxes' );
		
		$this->ipsclass->adskin->td_header[] = array( "Money Range"   , "" );
		$this->ipsclass->adskin->td_header[] = array( "Time Interval" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Taxed"         , "" );
		$this->ipsclass->adskin->td_header[] = array( "Option"        , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Money on Hand");
		
		if(is_array($this->ipsclass->cache['market_taxes']['hand']) and count($this->ipsclass->cache['market_taxes']['hand']))
		{
			foreach($this->ipsclass->cache['market_taxes']['hand'] as $tax)
			{
				$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																					$this->ipsclass->numbers->format_currency($tax['range_min'])." to ".($tax['range_max'] <= 0 ? "Infinite" : $this->ipsclass->numbers->format_currency($tax['range_max'])),
																					$this->ipsclass->numbers->time_string($tax['interval'],'SHORT'),
																					$this->ipsclass->numbers->format_currency($tax['tax_fix'])." plus ".$this->ipsclass->numbers->do_number_format($tax['tax_percent'],2)."% money over ".$this->ipsclass->numbers->format_currency($tax['tax_psub']),
																					"<img id=\"menu_{$tax['t_id']}\" src='{$this->ipsclass->vars['board_url']}/skin_acp/IPB2_Standard/images/filebrowser_action.gif' border='0' alt='Options' class='ipd' style='float:right'/>",
																			)	);
				
				$this->ipsclass->html .= "<script type=\"text/javascript\">
	menu_build_menu( \"menu_{$tax['t_id']}\",
  new Array(
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&code=taxes&do=edit&tax=".$tax['t_id']."'>Edit Tax</a>\",
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&code=taxes&do=delete&tax=".$tax['t_id']."'>Delete Tax</a>\"
      )     );
 </script>";
			}
		}
		else
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No money on hand taxes",'center');
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->adskin->td_header[] = array( "Money Range"   , "" );
		$this->ipsclass->adskin->td_header[] = array( "Time Interval" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Taxed"         , "" );
		$this->ipsclass->adskin->td_header[] = array( "Option"        , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Post-Gained Money");
		
		if(is_array($this->ipsclass->cache['market_taxes']['post']) and count($this->ipsclass->cache['market_taxes']['post']))
		{
			foreach($this->ipsclass->cache['market_taxes']['post'] as $tax)
			{
				$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																					$this->ipsclass->numbers->format_currency($tax['range_min'])." to ".($tax['range_max'] <= 0 ? "Infinite" : $this->ipsclass->numbers->format_currency($tax['range_max'])),
																					$this->ipsclass->numbers->time_string($tax['interval'],'SHORT'),
																					$this->ipsclass->numbers->format_currency($tax['tax_fix'])." plus ".$this->ipsclass->numbers->do_number_format($tax['tax_percent'],2)."% money over ".$this->ipsclass->numbers->format_currency($tax['tax_psub']),
																					"<img id=\"menu_{$tax['t_id']}\" src='{$this->ipsclass->vars['board_url']}/skin_acp/IPB2_Standard/images/filebrowser_action.gif' border='0' alt='Options' class='ipd' style='float:right'/>",
																			)	);
				
				$this->ipsclass->html .= "<script type=\"text/javascript\">
	menu_build_menu( \"menu_{$tax['t_id']}\",
  new Array(
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&code=taxes&do=edit&tax=".$tax['t_id']."'>Edit Tax</a>\",
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&code=taxes&do=delete&tax=".$tax['t_id']."'>Delete Tax</a>\"
      )     );
 </script>";
			}
		}
		else
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No post-gained money",'center');
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->tax_form();
		
		$this->ipsclass->admin->output();
	}
	
	function tax_form($tax_id = 0)
	{
		if($tax_id == 0)
		{
			$this->ipsclass->cache['market_taxes']['id'][0]['applying'] = 'post';
			$this->ipsclass->cache['market_taxes']['id'][0]['t_week'] = 0;
			$this->ipsclass->cache['market_taxes']['id'][0]['t_day'] = 0;
			$this->ipsclass->cache['market_taxes']['id'][0]['t_hour'] = 0;
			$this->ipsclass->cache['market_taxes']['id'][0]['t_minute'] = 0;
			$this->ipsclass->cache['market_taxes']['id'][0]['tax_fix'] = 0;
			$this->ipsclass->cache['market_taxes']['id'][0]['tax_percent'] = 0;
			$this->ipsclass->cache['market_taxes']['id'][0]['tax_pmin'] = 0;
			$this->ipsclass->cache['market_taxes']['id'][0]['tax_psub'] = 0;
			$this->ipsclass->cache['market_taxes']['id'][0]['range_min'] = 0;
			$this->ipsclass->cache['market_taxes']['id'][0]['range_max'] = 0;
		}
		
		$minute = array();
		for($i = 0; $i < 60; $i++)
		{
			$minute[$i] = array($i,$i." minutes");
		}
		$minute[1] = array(1, "1 minute (:01)");
		
		$hours = array();
		for($i = 0; $i < 24; $i++)
		{
			$hours[$i] = array($i, $i." hours");
		}
		$hours[1] = array(1, "1 hour");
		
		$days = array();
		$days[0] = array(0, "0 days");
		$days[1] = array(1, "1 day");
		$days[2] = array(2, "2 days");
		$days[3] = array(3, "3 days");
		$days[4] = array(4, "4 days");
		$days[5] = array(5, "5 days");
		$days[6] = array(6, "6 days");
		
		$weeks = array();
		$weeks[0] = array(0, "0 weeks");
		$weeks[1] = array(1, "1 week");
		$weeks[2] = array(2, "2 weeks");
		$weeks[3] = array(3, "3 weeks");
		$weeks[4] = array(4, "4 weeks");
		//$weeks[5] = array(5, "5 weeks");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'             ),
																			 2 => array( 'act'       , 'market'                 ),
																			 3 => array( 'code'      , 'taxes'                  ),
																			 4 => array( 'do'        , $tax_id ? 'save' : 'add' ),
																			 5 => array( 'tax'       , $tax_id                  ),
																	)		);
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table($tax_id ? "Edit a Regular Tax" : "Add a Regular Tax");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>What is this tax applied to?</b>",
																			  $this->ipsclass->adskin->form_dropdown("applying",array(array('post','Post-Gained Money'),array('hand','Money on Hand')),$this->ipsclass->cache['market_taxes']['id'][$tax_id]['applying'])
																	 )      );

		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>How often is tax applied?</b><br />The tax would be applied every amount of the time specified",
																			  $this->ipsclass->adskin->form_dropdown("t_week", $weeks, $this->ipsclass->cache['market_taxes']['id'][$tax_id]['t_week'])."<br />".$this->ipsclass->adskin->form_dropdown("t_day", $days, $this->ipsclass->cache['market_taxes']['id'][$tax_id]['t_day'])."<br />".$this->ipsclass->adskin->form_dropdown("t_hour", $hours, $this->ipsclass->cache['market_taxes']['id'][$tax_id]['t_hour'])."<br />".$this->ipsclass->adskin->form_dropdown("t_minute", $minute, $this->ipsclass->cache['market_taxes']['id'][$tax_id]['t_minute']),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>What range of money is this tax is applied to?</b><br />Input 0 or infinite on the maximum (second input) for no limit on the max.",
																			  "Min ".$this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input("range_min",$this->ipsclass->cache['market_taxes']['id'][$tax_id]['range_min'],10))." to max ".$this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input("range_max",$this->ipsclass->cache['market_taxes']['id'][$tax_id]['range_max'],10)),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Tax Fixed Amount</b><br />This amount does not relate to how much is money applied.",
																			  $this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input("tax_fix",$this->ipsclass->cache['market_taxes']['id'][$tax_id]['tax_fix'],10)),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Tax Percentage Amount</b><br />This amount does money applied over the amount specified below.",
																			  $this->ipsclass->adskin->form_simple_input("tax_percent",$this->ipsclass->cache['market_taxes']['id'][$tax_id]['tax_percent'],10)."%",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Tax Percentage Appling Amount</b><br />For the tax percentage, the percent amount is applied to the amount over the specified amount.<br />For example you say 5% over <u>".$this->ipsclass->numbers->currency_format(100)."</u> for a person who have ".$this->ipsclass->numbers->currency_format(500).",<br />the amount applied to the percentage is ".$this->ipsclass->numbers->currency_format(400)." and the tax percentage is ".$this->ipsclass->numbers->currency_format(20).".",
																			  $this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input("tax_psub",$this->ipsclass->cache['market_taxes']['id'][$tax_id]['tax_psub'],10)),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Tax Percentage Appling Minimum</b><br />For the tax percentage, the percentage  is applied when the amount is over this input",
																			  $this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input("tax_pmin",$this->ipsclass->cache['market_taxes']['id'][$tax_id]['tax_pmin'],10)),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form(($tax_id ? "Save" : "Add")." tax");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
	}
	
	function add_tax()
	{
		if($this->ipsclass->input['tax'] != 0)
		{
			$this->ipsclass->admin->error("Invalid Action");
		}
		
		$this->parse_input();
		
		$this->ipsclass->DB->do_insert('market_taxes',$this->ipsclass->cache['market_taxes']['id'][0]);
		
		$this->ipsclass->market_class->cache_taxes();
		
		$this->ipsclass->admin->save_log("ibMarket: Add tax");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=taxes","Tax added...");
	}
	
	function parse_input()
	{
		$this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['applying'] = in_array($this->ipsclass->input['applying'],array('post','hand')) ? $this->ipsclass->input['applying'] : 'posts';
		$this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['t_week'] = $this->ipsclass->input['t_week'];
		$this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['t_day'] = $this->ipsclass->input['t_day'];
		$this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['t_hour'] = $this->ipsclass->input['t_hour'];
		$this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['t_minute'] = $this->ipsclass->input['t_minute'];
		$this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['tax_fix'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['tax_fix'],'int');
		$this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['tax_percent'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['tax_percent'],'float');
		$this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['tax_pmin'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['tax_pmin'],'int');
		$this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['tax_psub'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['tax_psub'],'int');
		$this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['range_min'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['range_min'],'int');
		$this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['range_max'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['range_max'],'int');
	}
	
	function edit_tax()
	{
		$this->ipsclass->input['tax'] = intval($this->ipsclass->input['tax']);
		if(!is_array($this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]))
		{
			$this->ipsclass->admin->error("Cannot find the tax you are trying to edit");
		}
		
		$this->ipsclass->admin->page_title  = "Welcome to editing a tax";
		$this->ipsclass->admin->page_detail = "Welcome to editing a tax.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=taxed', 'Manage Taxes' );
		$this->ipsclass->admin->nav[] 		= array( ''                                          , 'Edit Tax'     );
		
		$this->tax_form($this->ipsclass->input['tax']);

		$this->ipsclass->admin->output();
	}
	
	function save_tax()
	{
		$this->ipsclass->input['tax'] = intval($this->ipsclass->input['tax']);
		if(!is_array($this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]))
		{
			$this->ipsclass->admin->error("Cannot find the tax you are trying to edit");
		}
		
		$this->parse_input();
		
		$this->ipsclass->DB->do_update('market_taxes',array(
																'applying' => $this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['applying'],
																't_week' => $this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['t_week'],
																't_day' => $this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['t_day'],
																't_hour' => $this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['t_hour'],
																't_minute' => $this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['t_minute'],
																'tax_fix' => $this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['tax_fix'],
																'tax_percent' => $this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['tax_percent'],
																'tax_pmin' => $this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['tax_pmin'],
																'tax_psub' => $this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['tax_psub'],
																'range_min' => $this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['range_min'],
																'range_max' => $this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]['range_max'],
					),"t_id=".$this->ipsclass->input['tax']);
		
		$this->ipsclass->market_class->cache_taxes();
		
		$this->ipsclass->admin->save_log("ibMarket: Editted tax");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=taxes","Tax edited and saved...");
	}
	
	function delete_tax()
	{
		$this->ipsclass->input['tax'] = intval($this->ipsclass->input['tax']);
		if(!is_array($this->ipsclass->cache['market_taxes']['id'][$this->ipsclass->input['tax']]))
		{
			$this->ipsclass->admin->error("Cannot find the tax you are trying to delete");
		}
		
		$this->ipsclass->DB->build_and_exec_query(array('delete'=>'market_taxes','where'=>"t_id=".$this->ipsclass->input['tax']));
		
		$this->ipsclass->market_class->cache_taxes();
		
		$this->ipsclass->admin->save_log("ibMarket: Deleted tax");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=taxes","Tax deleted...");
	}
	
	function collected_taxes()
	{
		$LIMIT = 50;
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$this->ipsclass->admin->page_title  = "Welcome to collected taxes page";
		$this->ipsclass->admin->page_detail = "Welcome to collected taxes page";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=taxes&amp;do=log', 'Collected Taxes' );
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_tax_logs','where'=>"amount != 0"));
		$page = $this->ipsclass->adskin->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'USE_ST'       => 'st',
																	'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																	'L_SINGLE'     => "Single Page",
																	'BASE_URL'     => $this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;code=taxes&amp;do=log"
															)	);

		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->html .= "<br />";
		
		$this->ipsclass->adskin->td_header[] = array( "Who"    , "" );
		$this->ipsclass->adskin->td_header[] = array( "When"   , "" );
		$this->ipsclass->adskin->td_header[] = array( "Amount" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Collected Taxes");
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'l.*',
												'from' => array('market_tax_logs'=>'l'),
												'add_join' => array(
																		array(
																				'select' => 'm.members_display_name,m.mgroup',
																				'from' => array('members'=>'m'),
																				'where' => 'l.member_id=m.id',
																				'type' => 'left'
																			),
																	),
												'where' => "amount != 0",
												'order' => 'l.l_time DESC',
												'limit' => array($this->ipsclass->input['st'],$LIMIT)
										)	);
		$this->ipsclass->DB->exec_query();
		while($log = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  $this->ipsclass->market_class->format_name($log['member_id'],$log['members_display_name'],$log['mgroup']),
																				  $this->ipsclass->get_date($log['l_time'],'LONG'),
																				  $this->ipsclass->numbers->format_currency($log['amount'])
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->admin->output();
	}
}
?>